package define

import (
	"encoding/base64"
	"github.com/sirupsen/logrus"
	"regexp"
	"time"
)

const (
	SmtpAddress       = "smtp.feishu.cn"
	SmtpPort          = 587
	SmtpPassword      = "2lvEay82P8Q36gA4"
	PublicUserMail    = "mobaijie@bytedance.com"
	InputEmlFilePath  = "/Users/bytedance/go/src/code.byted.org/mobaijie/sender/define/send_test.eml"
	OutputEmlFilePath = "/Users/bytedance/go/src/code.byted.org/mobaijie/sender/output/output_%s_%d_eml.eml"
	UrlPrefix         = "https://sendbenewake.oss-cn-guangzhou.aliyuncs.com/"
	EndPoint          = "oss-cn-guangzhou.aliyuncs.com"
	Key               = "LTAI5tAVXqRWCGuArdD17Y3z"
	Secret            = "NGPEXQadNVavICjykMbLEvoWLrLR1D"
	Bucket            = "sendbenewake"
	PicAttachmentHead = `------=_NextPart_6281C5B5_10191940_2475BE63
Content-Type: %s
Content-Transfer-Encoding: base64
Content-ID: %s

%s` // 图片文件附件模版
	StreamAttachmentHead = `------=_NextPart_6281C5B5_10191940_2475BE63
Content-Type: %s
Content-Disposition: attachment; filename="%s"
Content-Transfer-Encoding: base64

%s` // 其他文件流附件模版
	EmlTemplate = `From: <%s>
To: %s
Subject: %s
Mime-Version: 1.0
Content-Type: multipart/related;
	type="multipart/alternative";
	boundary="----=_NextPart_6281C5B5_10191940_2475BE63"
Content-Transfer-Encoding: 8Bit
Date: Mon, 16 May 2022 11:32:05 +0800

This is a multi-part message in MIME format.

------=_NextPart_6281C5B5_10191940_2475BE63
Content-Type: multipart/alternative;
	boundary="----=_NextPart_6281C5B5_10191940_345FB571";

------=_NextPart_6281C5B5_10191940_345FB571
Content-Type: text/plain;
	charset="ISO-8859-1"
Content-Transfer-Encoding: base64

YWJjZGU=

------=_NextPart_6281C5B5_10191940_345FB571
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: base64

%s

------=_NextPart_6281C5B5_10191940_345FB571--

%s

------=_NextPart_6281C5B5_10191940_2475BE63--

` // eml模版
)

const (
	ContentType = "Content-Type"
	ContentID   = "Content-ID"
)

const (
	SendAllContent      = "Send allContent"
	SendOnlyImages      = "Send images"
	SendOnlyLinks       = "Send links"
	SendOnlyPlainText   = "Send plain text"
	SendOnlyAttachments = "Send attachments"
)

// 日志对象
var log = logrus.New()

// 正则对象
var (
	MailRegexp   = regexp.MustCompile(`^[a-zA-Z0-9!-/]+@[a-zA-Z0-9!-/]+(\.[a-zA-Z0-9_-]+)+$`)
	MailToRegexp = regexp.MustCompile(`^mailto:[a-zA-Z0-9!-/]+@[a-zA-Z0-9!-/]+(\.[a-zA-Z0-9_-]+)+$`)
)

// 收信人列表
var RcptList []string = []string{
	//"mobaijie@bytedance.com",
	"1102067530@qq.com",
	//"luisyu.buffer@gmail.com",
	//"yuyi54321@qq.com",
	//"yballul@163.com",
	//"yuyi304738837@163.com",
}

// smtp的请求与响应类
type SmtpReq struct {
	Tag            PartTag  `json:"tag"`
	SeqId          int64    `json:"seq_id"`
	MailFrom       string   `json:"mail_from"`
	RcptTo         []string `json:"rcpt_to"`
	HtmlBody       string   `json:"html_body"`
	Subject        string   `json:"subject"`
	AttachmentBody string   `json:"attachment_body"`
}

type PartTag string

const (
	AllContentTag PartTag = "all_content"
	ImageTag      PartTag = "image"
	LinkTag       PartTag = "link"
	AttachmentTag PartTag = "attachment"
	PlainTextTag  PartTag = "plain_text"
)

func NewSmtpReq(htmlBody string, attachmentBody string, tag PartTag) *SmtpReq {
	return &SmtpReq{
		Tag:            tag,
		SeqId:          time.Now().Unix(),
		MailFrom:       PublicUserMail,
		RcptTo:         RcptList,
		HtmlBody:       base64.StdEncoding.EncodeToString([]byte(htmlBody)),
		Subject:        "",
		AttachmentBody: attachmentBody,
	}
}

type SmtpResp struct {
	SeqId         int64             `json:"seq_id"`
	Headers       map[string]string `json:"headers"`
	StatusCode    RespCode          `json:"status_code"`
	StatusMessage string            `json:"status_message"`
}

type RespCode int

const (
	Success     = 0
	ParamErr    = 10001
	SendMailErr = 10002
)

var codeMessageMap map[RespCode]string = map[RespCode]string{
	Success:     "OK",
	ParamErr:    "ParamInvalid",
	SendMailErr: "SendMailFail",
}

func GetMessage(code RespCode) string {
	val, _ := codeMessageMap[code]
	return val
}

// 从eml中提取出来的需要发送的相关内容
type EmlParseData struct {
	AllContent        string   `json:"all_content"`       //邮件正文全部内容
	PlainText         string   `json:"plain_text"`        // 正文纯文字
	StreamAttachments []string `json:"attachments"`       // 邮件的全部附件
	ImageAttachments  []string `json:"image_attachments"` // 邮件全部图片附件内容
}
